/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bungabungaball.gamestates;

import bungabungaball.GlobalSettings;
import bungabungaball.gamestates.menu.Menu;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import bungabungaball.gamestates.menu.MenuEntry;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.newdawn.slick.Image;

/**
 * The options for BungaBungaBall
 *
 * @author Ingo Klose, Stephan Meyer, Sebastian Lehmann
 * @version 1.0
 */
public class MenuOption extends BasicGameState implements Menu {

    /**
     * the id of the game state
     */
    private int stateID = -1;
    /**
     * the mouse position x
     */
    private int mouseX;
    /**
     * the mouse position y
     */
    private int mouseY;
    /**
     * *
     * menu entry for disco mode
     */
    private MenuEntry discoMode;
    /**
     * *
     * menu entry for sound
     */
    private MenuEntry sound;
    /**
     * *
     * men entry for going back to main menu
     */
    private MenuEntry back;
    /**
     * An image of the driver
     */
    private Image driver;
    /**
     * An image of the driver
     */
    private Image driver2;
    /**
     * Bunga Logo
     */
    private Image bunga;
    /**
     * Bunga Logo
     */
    private Image bungamenu;
    /**
     * x Position of the driver
     */
    private double driverX;
    /**
     * Y Position of the driver
     */
    private double driverY;
    /**
     * A counter for the driver
     */
    private double counter = 0;
    /**
     * A checker for the driver
     */
    private boolean checker = true;

    /**
     * Constructor
     *
     * @param stateID
     */
    public MenuOption(int stateID) {
        this.stateID = stateID;
        driverX = 600;
        driverY = 350;
    }

    /**
     * get the ID of the state
     *
     * @return id of the state
     */
    @Override
    public int getID() {
        return stateID;
    }

    /**
     * Init all Menus
     *
     * @param gc
     * @param sbg
     * @throws SlickException
     */
    @Override
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
        discoMode = new MenuEntry();
        discoMode.text = GlobalSettings.discoMode ? "DiscoMode: ein" : "DiscoMode: aus";
        discoMode.x = 100;
        discoMode.y = 150;
        discoMode.w = 150;
        discoMode.h = 15;

        sound = new MenuEntry();
        sound.text = GlobalSettings.sound ? "Sound: ein" : "Sound: aus";
        sound.x = 100;
        sound.y = 170;
        sound.w = 150;
        sound.h = 15;

        back = new MenuEntry();
        back.text = "Zurück";
        back.x = 100;
        back.y = 210;
        back.w = 80;
        back.h = 15;

        driver = new Image("\\img\\player1.png");

        driver2 = new Image("\\img\\player2.png");
        driver2.setRotation(45);

        bunga = new Image("\\img\\bungabunga.png");
    }

    /**
     * Render the Option menu
     *
     * @param gc
     * @param sbg
     * @param g
     * @throws SlickException
     */
    @Override
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
        g.setColor(Color.red);
        g.setFont(MainMenu.font);
        g.drawString("Optionen", 100, 100);

        g.setColor(discoMode.checkMouseIn(mouseX, mouseY) ? Color.green : Color.white);
        g.drawString(discoMode.text, discoMode.x, discoMode.y);

        g.setColor(sound.checkMouseIn(mouseX, mouseY) ? Color.green : Color.white);
        g.drawString(sound.text, sound.x, sound.y);

        g.setColor(back.checkMouseIn(mouseX, mouseY) ? Color.green : Color.white);
        g.drawString(back.text, back.x, back.y);

        g.drawImage(driver, (int) driverX, (int) driverY);

        g.drawImage(driver2, 200, 400);


        g.drawImage(bunga, 400, 0);

    }

    /**
     * handle all input
     *
     * @param gc
     * @param sbg
     * @param delta
     * @throws SlickException
     */
    @Override
    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
        Input input = gc.getInput();

        if (input.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON)) {
            if (discoMode.checkMouseIn(mouseX, mouseY)) {
                discoMode.clickPause();
                GlobalSettings.discoMode = !GlobalSettings.discoMode;
                discoMode.text = GlobalSettings.discoMode ? "DiscoMode: ein" : "DiscoMode: aus";
            }
            if (sound.checkMouseIn(mouseX, mouseY)) {
                sound.clickPause();
                GlobalSettings.sound = !GlobalSettings.sound;
                sound.text = GlobalSettings.sound ? "Sound: ein" : "Sound: aus";
            }
            if (back.checkMouseIn(mouseX, mouseY)) {
                discoMode.clickPause();
                sbg.enterState(bungabungaball.BungaBungaBall.MAINMENUSTATE);
            }
        }

                if (input.isKeyDown(Input.KEY_ESCAPE)) {
            try {
                Thread.sleep(250);
            } catch (InterruptedException ex) {
                Logger.getLogger(GamePlay.class.getName()).log(Level.SEVERE, null, ex);
            }
            sbg.enterState(bungabungaball.BungaBungaBall.MAINMENUSTATE);
        }

        mouseX = input.getAbsoluteMouseX();
        mouseY = input.getAbsoluteMouseY();

        this.drive();
        this.drive();
    }

    /**
     * Updates the driver Image
     */
    public void drive() {
        //x = 600 y = 400
        double speedRotation = Math.toRadians(driver.getRotation() - 90.f);

        driverX = driverX + 2 * Math.cos(speedRotation);
        driverY = driverY + 2 * Math.sin(speedRotation);

        if (checker && counter <= 270) {
            driver.rotate(1);
        } else if (!checker && counter <= 270) {
            driver.rotate(-1);
        }
        counter++;
        if (counter == 385) {
            counter = 0;
            if (checker) {
                checker = false;
            } else {
                checker = true;
            }
        }
    }
}
